#!/bin/bash

# Slackware build script for fontforge

# Copyright 2006-2012,2016  Robby Workman  Tuscaloosa, Alabama, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Currently maintained by David Woodfall <dave@slackbuilds.org>
#
# 2022-02-08 19:11:35 +0000,20201107
# Switch to cmake
#
# 2020-10-12 20:48:40 +0100:
# Update to Version ff 20190801, gnulib 20200505~0747bf0
# Added --without-libuninameslist

# 20220409 bkw: Modified by SlackBuilds.org, BUILD=2:
# - make PRINT_PACKAGE_NAME work even if fontforge already installed.
# - get rid of /usr/share/doc.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=fontforge
VERSION=${VERSION:-20201107}
BUILD=${BUILD:-3}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

ENABLE_BCINT=${ENABLE_BCINT:-yes}
FREETYPE=${FREETYPE:-2.11.1}
GNULIB=${GNULIB:-0.1}
UTHASH=${UTHASH:-2.3.0}

if [ -x /usr/bin/sphinx-build ]; then
  DOCS=ON
fi

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

# This ought to be more reliable than testing if a package DB file
# exists.  A little convoluted perhaps.
if $PRGNAM -v 2>/dev/null |
     sed -n "s,^\($PRGNAM .*\),Found existing installation:\n \n\1,p
     n;p;a\ " | grep -v "^$"; then

  echo "Please uninstall any previous versions of $PRGNAM before building
a new one."
  exit 1
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION freetype-$FREETYPE
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
tar xvf $CWD/freetype-$FREETYPE.tar.xz
chown -R root:root $PRGNAM-$VERSION freetype-$FREETYPE
chmod -R u+w,go+r-w,a-s $PRGNAM-$VERSION freetype-$FREETYPE
cd $PRGNAM-$VERSION
tar xvf $CWD/gnulib-$GNULIB.tar.gz
tar xvf $CWD/uthash-$UTHASH.tar.gz
mv gnulib-"$GNULIB" gnulib
mv uthash-"$UTHASH" uthash
chown -R root:root gnulib uthash
chmod -R u+w,go+r-w,a-s gnulib uthash

mkdir -p build
cd build
  cmake \
    -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DLIB_SUFFIX=${LIBDIRSUFFIX} \
    -DMAN_INSTALL_DIR=/usr/man \
    -DENABLE_LIBSPIRO=OFF \
    -DENABLE_LIBUNINAMESLIST=OFF \
    -DENABLE_DOCS=${DOCS:-OFF} \
    -DCMAKE_BUILD_TYPE=Release ..
  make
  make install DESTDIR=$PKG
cd ..

# Let's add the cidmaps
mkdir -p $PKG/usr/share/fontforge
tar xvf $CWD/cidmaps.tgz -C $PKG/usr/share/fontforge || exit 1
chmod 0644 $PKG/usr/share/fontforge/*.cidmap
chown root:root $PKG/usr/share/fontforge/*.cidmap

rm -f $PKG/{,usr/}lib${LIBDIRSUFFIX}/*.la

find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | \
  grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

mv $PKG/usr/share/man $PKG/usr
find $PKG/usr/man -type f -exec gzip -9 {} \;
for i in $( find $PKG/usr/man -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION

# Move docs
if [ -d $PKG/usr/share/doc/$PRGNAM/ ]; then
  mv $PKG/usr/share/doc/$PRGNAM/* $PKG/usr/doc/$PRGNAM-$VERSION
  rm -rf $PKG/usr/share/doc
fi

cp -a AUTHORS LICENSE $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
find $PKG/usr/doc -type f -exec chmod 644 {} \+

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
